<template>
  <div>
    <!--  搜索框  -->
    <div>
      <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
        <el-form-item label="用户名称" prop="userName">
          <el-input
            v-model="queryParams.userName"
            placeholder="请输入用户名称"
            clearable
            size="small"
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="用户昵称" prop="nickName">
          <el-input
            v-model="queryParams.nickName"
            placeholder="请输入用户名称"
            clearable
            size="small"
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="手机号码" prop="phonenumber">
          <el-input
            v-model="queryParams.phonenumber"
            placeholder="请输入手机号码"
            clearable
            size="small"
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column v-if="!single" type="selection" width="50" align="center" />
      <el-table-column label="用户编号" align="center" prop="userId" />
      <el-table-column label="用户名称" align="center" prop="userName" :show-overflow-tooltip="true" />
      <el-table-column label="用户昵称" align="center" prop="nickName" :show-overflow-tooltip="true" />
      <el-table-column label="手机号码" align="center" prop="phonenumber" width="120" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="handleSingle(scope.row)"
          >选择</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { listUserWithScope } from '@/api/system/user'
export default {
  name: 'CSelectUser',
  props: {
    scope: String,
    single: Boolean
  },
  data() {
    return {
      userList: [],
      total: 0,
      loading: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        nickName: undefined,
        phonenumber: undefined
      },
      // 选中的数组
      ids: [],
      showSearch: true,
      user: {
        id: undefined,
        nickName: undefined
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.queryParams.scope = this.scope
      listUserWithScope(this.queryParams).then(response => {
        console.log(response.rows)
        this.userList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 选中 用户后传递 ids
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId)
      this.$emit('selectIds', this.ids)
    },
    // 选中返回一个 user
    handleSingle(user) {
      this.user.id = user.userId
      this.user.nickName = user.nickName
      this.$emit('selectSingle', this.user)
    },
    handleQuery() {
      this.queryParams.page = 1
      this.getList()
    },
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        nickName: undefined,
        phonenumber: undefined
      }
    }
  }
}
</script>

<style>

</style>
